﻿/*
面頁級驗證腳本
*/
//<script src="../ScriptFiles/JS/jquery.js" type="text/javascript"></script>
function GetCheckCtlId(pageRuleHiddenId) {
    var ss = pageRuleHiddenId.toString();
    var str = "_page_rule";
    var val = "";
    var endIndex = (ss.indexOf(str));
    if (endIndex > -1)
        val = ss.substr(0, endIndex);
    return val;
}

function GetPageRuleProValue(proInfo, proName) {
    var proFG = ",", pro_valFG = ":";
    var val = "";
    var pros = proInfo.toString().split(proFG);
    for (var i = 0; i < pros.length; i++) {
        var p = String(pros[i]);
        var pro_valFGIndex = p.indexOf(pro_valFG, 0);
        if (pro_valFGIndex > 1 && pro_valFGIndex < (p.length - 1)) {
            var strProName = p.substring(0, pro_valFGIndex);
            val = p.substring(pro_valFGIndex + 1, p.length);
            if (strProName == proName) {
                return val;
            }
        }
    }
    return val;
}

var errMsgId = "page_checkMsg";
var errTxtBgColor = "yellow";

function GetOneRuleMsg(checkObjProName, txtValue, proInfo) {
    var strMsg = "";
    if (proInfo != "") {
        var typeName = GetPageRuleProValue(proInfo, "TypeName");
        
        if (typeName == "必填") {
            if (txtValue == "") {
                strMsg = " 【 " + checkObjProName + " 】 " + typeName;
            }
        }
        else {
            if (typeName.indexOf("長度限制", 0) > -1) {
                var len = typeName.substring(5, typeName.toString().length);
                if (txtValue != "") {
                    if (txtValue.toString().length > len) {
                        if (checkObjProName != "")
                            strMsg = " 【 " + checkObjProName + " 】 長度不能超過"+ len + ",實際值為: 【 " + txtValue.toString().length + " 】 ";
                    }
                }
            }
            else {
                var regExpValue = GetPageRuleProValue(proInfo, "RegExpValue");
                var errMsg = GetPageRuleProValue(proInfo, "ErrMsg");
                if (txtValue != "") {
                    var reg = RegExp(regExpValue);
                    if (!reg.test(txtValue)) {
                        if (checkObjProName != "")
                            strMsg = " 【 " + checkObjProName + " 】 " + errMsg + ",實際值為: 【 " + txtValue + " 】 ";
                    }
                }
            }
        }
    }
    return strMsg;
}
var pevErrStr = "";
function SubCheck(checkCtlId, proInfo) {
    var typeName = GetPageRuleProValue(proInfo, "TypeName");
    var ctl = $("#" + checkCtlId);

    if (typeName != "") {
        var txtValue = ctl.val();
        var bgColor = ctl.css("background-color") ; 
        if(bgColor!= errTxtBgColor) 
            bgColor = "#FFFFFF";
        var errMsg = $("#" + errMsgId);
        var checkObjProName = (ctl.parent("td").prev().text());
        if (checkObjProName == "")
            checkObjProName = (ctl.parents().parent("td").prev().text());
        var errStr = GetOneRuleMsg(checkObjProName, txtValue, proInfo).toString();

        if (errStr != "") {
            bgColor = errTxtBgColor;
            if (errStr != pevErrStr) {
                pevErrStr = errStr;
                errMsg.val(errMsg.val() + errStr + "\r\n");
                ctl.attr("title", errStr);
            }
        }
        ctl.css("background-color", bgColor.toString());
    }
}

function SetCtlCheck() {
    var errMsg = $("#" + errMsgId);
    errMsg.val("");
    pevErrStr = "";
    var pageRuleHiddens = $(document).find("input[class='page_rule']");
    pageRuleHiddens.each(function(i) {
        var id = $(this).attr("id");
        var proInfo = $(this).val();
        var checkCtlId = GetCheckCtlId(id);
        if (checkCtlId.length > 0) {
            var checkCtl = $("#" + checkCtlId);
            if ($("input[type='text'][name^='" + checkCtlId + "']").size() > 0) {
                $("input[type='text'][name^='" + checkCtlId + "']").each(function(i) {
                    SubCheck($(this).attr("id"), proInfo);
                });
            }
            else {
                SubCheck(checkCtlId, proInfo);
            }

        }
    });
    pageRuleHiddens.each(function(i) {
        var amount = pageRuleHiddens.size();
        var obj = pageRuleHiddens.eq(amount - i - 1);
        var id = obj.attr("id");
        var checkCtlId = GetCheckCtlId(id);

        if (checkCtlId.length > 0) {
            var checkCtl = $("#" + checkCtlId);
            if (checkCtl.css("background-color") == errTxtBgColor)
                checkCtl.select();
        }
    });

}

function AddTip() {
    if ($("input[class='page_rule'][value='TypeName:必填']").size() > 0) {
        $("input[class='page_rule'][value='TypeName:必填']").each(function(i) {
            $(this).parent().append("<font Color='red' >*</font>");
        });
    }
}

